package com.example.demo.controller;

import com.example.demo.annotate.NoCheckToken;
import com.example.demo.annotate.Permission;
import com.example.demo.common.Result;
import com.example.demo.common.constant.RuleCode;
import com.example.demo.entity.dto.LoginerDTO;
import com.example.demo.entity.dto.UserDTO;
import com.example.demo.service.UserService;
import com.example.demo.verify.ParamValid;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

@RestController
@RequestMapping("/api/user")
@Api(tags = "14、用户操作管理")
@ApiSupport(author = "罗海林", order = 14)
@Validated
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @PostMapping("/getUserInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id编号")
    })
    @ApiOperation(value = "获取单个用户信息", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result getUserInfo(@ParamValid(name = "id编号", rule = RuleCode.num) @RequestParam int id) {
        return userService.getUserInfo(id);
    }

    @PostMapping("/loginToken")
    @ApiOperation(value = "登录，保存token", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    @Permission(identity = {"admin", "my"})
    public Result loginToken(@RequestBody @Validated LoginerDTO loginerDTO) {
        return userService.loginToken(loginerDTO);
    }

    @PostMapping("/loginToken2")
    @ApiOperation(value = "登录，保存token（无userid）", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    @NoCheckToken
    public Result loginToken2(@RequestBody @Validated LoginerDTO loginerDTO) {
        return userService.loginTokenNoUserId(loginerDTO);
    }

    @PostMapping("/loginJwt")
    @ApiOperation(value = "登录，保存JWT", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    @NoCheckToken
    public Result loginJwt(@RequestBody @Validated LoginerDTO loginerDTO) {
        return userService.loginJwt(loginerDTO);
    }

    @PostMapping("/loginJson")
    @ApiOperation(value = "登录，保存JSON", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    public Result loginJson(@RequestBody @Validated LoginerDTO loginerDTO) {
        return userService.loginJson(loginerDTO);
    }

    @PostMapping("/getUserList")
    @ApiOperation(value = "获取用户列表", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 5)
    public Result getUserList(@RequestBody @Validated UserDTO userDTO) {
        return userService.getUserList(userDTO);
    }

    @PostMapping("/insertUser")
    @ApiOperation(value = "批量插入用户信息", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 6)
    public Result insertUser() {
        return userService.insertUser();
    }

    @PostMapping("/insertUserByJDBC")
    @ApiOperation(value = "批量插入用户信息（JDBC）", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 7)
    public Result insertUserByJDBC() throws SQLException {
        return userService.insertUserByJDBC();
    }

    @PostMapping("/insertUserByMyBatis")
    @ApiOperation(value = "批量插入用户信息（MyBatis）", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 8)
    public Result insertUserByMyBatis() {
        return userService.insertUserByMyBatis();
    }

    @PostMapping("/JDBC")
    @ApiOperation(value = "原生JDBC封装", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 9)
    public Result JDBC() {
        return userService.JDBC();
    }

    @PostMapping("/sensitiveUser")
    @ApiOperation(value = "脱敏用户信息", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 100)
    public Result sensitiveUser() {
        return userService.sensitiveUser();
    }

}
